<?php

/**
 * IG_Controller_Action_Helper_Message
 *
 * @author mmxie
 * @copyright Copyright (c) 2009 Green Shore (http://www.iwgame.com)
 * @version $Id: Message.php 929 2011-10-27 07:52:50Z mmxie $
 */
class IG_Controller_Action_Helper_Message extends Zend_Controller_Action_Helper_Json
{
    /**
     * @var array
     */
    public $_attributes = array();

    public function sendJson($data = null, $keepLayouts = false)
    {
        if (is_null($data)) {
            $data = $this->_attributes;
        }
        return parent::sendJson($data, $keepLayouts);
    }

    /**
     * direct
     * @param array|string|null $data
     * @param bool $sendNow
     * @param bool $keepLayouts
     * @return IG_Controller_Action_Helper_Message|void
     */
    public function direct($data, $sendNow = true, $keepLayouts = false)
    {
        if (is_string($data)) {
            $data = array('msg' => $data);
        }

        parent::direct($data, $sendNow, $keepLayouts);
    }

    public function __call($method, $params)
    {
        if (empty($params))
            return $this->getAttribute($method);
        else
            return $this->setAttribute($method, $params[0]);
    }

    /**
     *
     * @param string $key
     * @param mixed $value
     */
    public function setAttribute($key, $value)
    {
        $this->_attributes[$key] = $value;
        return $this;
    }

    /**
     *
     * @param string $key
     * @return mixed
     */
    public function getAttribute($key)
    {
        if (isset($this->_attributes[$key])) {
            return $this->_attributes[$key];
        }
    }

    public function getAttributes()
    {
        return $this->_attributes;
    }

    /**
     *
     * @param array $attributes
     */
    public function setAttributes($attributes)
    {
        $this->_attributes = $attributes;
    }
}